<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">销售出库列表</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="单据编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入单据编码"
            clearable
            size="small"
            style="width:150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input
            v-model="queryParams.batchNumber"
            placeholder="请输入批次号"
            clearable
            size="small"
            style="width:150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="SKU名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入SKU名称"
            clearable
            size="small"
            style="width:150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售订单号" prop="businessCode">
          <el-input
            v-model="queryParams.businessCode"
            placeholder="请输入销售订单号"
            clearable
            size="small"
            style="width:150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售出库状态" prop="status" >
          <el-select
            v-model="queryParams.status"
            placeholder="全部"
            clearable
            size="small"
            style="width:100px;"
          >
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="按起止时间" prop="time">
          <el-date-picker
              v-model="time"
              type="daterange"
              range-separator="至"
              style="width: 225px;"
              @change="dataTime()"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            size="small"
            style="width:150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作人" prop="updateBy">
          <el-input
            v-model="queryParams.updateBy"
            placeholder="请输入操作人"
            clearable
            size="small"
            style="width:150px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" class="list-search btn-success" color="#5ED4D4"
            @click="handleQuery"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" class="list-search"
            @click="resetQuery"
          >重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="23">
          <el-button
            class="btn-scanCodeDel"
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="btnExport()"
          >导出</el-button>
        </el-col>
        <el-col :span="1">
          <el-popover placement="bottom" title="筛选列" trigger="click" width="40">
            <el-checkbox-group v-model="checkedColumns" size="mini">
              <el-checkbox v-for="item in checkBoxGroup" :key="item" :label="item" :value="item"></el-checkbox>
            </el-checkbox-group>
            <div title="筛选列" class="filter-table-col" slot="reference">
              <img src="@/assets/img/screening.png" width="25" />
            </div>
          </el-popover>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="salesList" @row-click="handleView" @selection-change="handleSelectionChange" height="calc(100vh - 350px)" :key="reload" border>
        <el-table-column type="selection" width="35" align="center" />
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="(item, index) in tableHeaderData">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :align="item.align"
            :sortable="item.sortable"
            :show-overflow-tooltip="true"
            :key="index"
            :width="item.width"
            v-if="colData[index].isTrue"
          >
            <template slot-scope="scope">
              <template v-if="item.label == '状态'">
                <div v-if="scope.row.status == 0" style="color:#13C2C2">待出库</div>
                <div v-if="scope.row.status == 1" style="color:#52C41A">部分出库</div>
                <div v-if="scope.row.status == 2" style="color:#52C41A">已出库</div>
                <div v-if="scope.row.status == 3" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.status == 4" style="color:#fd562c">拒绝</div>
                <div v-if="scope.row.status == 5" style="color:#13C2C2">出库中</div>
              </template>
              <template v-else-if="item.label == '产品编码'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.productBasicCode }}
                </div>
              </template>
              <template v-else-if="item.label == 'SKU编码'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.productCode }}
                </div>
              </template>
              <template v-else-if="item.label == '产品名称'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.productBasicName }}
                </div>
              </template>
              <template v-else-if="item.label == '规格'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.productSpace }}
                </div>
              </template>
              <template v-else-if="item.label == '批次'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.batchNumber }}
                </div>
              </template>
              <template v-else-if="item.label == '单位'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.unit }}
                </div>
              </template>
              <template v-else-if="item.label == '订单数量'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.orderNum }}
                </div>
              </template>
              <template v-else-if="item.label == '实际出库数量'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.outStore }}
                </div>
              </template>
              <template v-else-if="item.label == '发票号'">
                <div v-for="(item, index) in scope.row.orderDetails" :key="index" class="item">
                  {{ item.invoice }}
                </div>
              </template>
              <span v-else>{{ scope.row[scope.column.property] }}</span>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { listOutSale } from "@/api/system/outSale/outSale"
import { exportProductOutOrder } from "@/api/system/export/export"

export default {
  name: "outSale",
  data() {
    return {
      loading: true,
      total: 0,
      salesList: [],
      status:[
        {
          value:'0',
          label:'待出库'
        },
        {
          value:'1',
          label:'部分出库'
        },
        {
          value:'2',
          label:'已出库'
        },
        {
          value:'3',
          label:'待审核'
        },
        {
          value:'4',
          label:'拒绝'
        },
        {
          value:'5',
          label:'出库中'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code:undefined,
        batchNumber:undefined,
        customerName: undefined,
        businessCode: undefined,
        status: undefined,
        updateBy: undefined,
        startTime: '',
        endTime: '',
        productName: undefined
      },
      ids: [],
      time: [],
      reload: '',
      tableHeaderData: [
        { label: "单据编码", align: "center", prop: "code", width: "150" },
        { label: "出库时间", align: "center", prop: 'endTime', width: "120" },
        { label: "状态", align: "center", prop: "statusName" },
        { label: "客户名称", align: "center", prop: "customerName", width: "150"},
        { label: "客户部门", align: "center", prop: "customerDept", width: "150" },
        { label: "产品编码", align: "center", prop: "productBasicCode", width: "150" },
        { label: "SKU编码", align: "center", prop: "productCode", width: "150" },
        { label: "产品名称", align: "center", prop: "productBasicName", width: "150" },
        { label: "规格", align: "center", prop: "productSpace", width: "120" },
        { label: "批次", align: "center", prop: "batchNumber", width: "100" },
        { label: "单位", align: "center", prop: "unit" },
        { label: "订单数量", align: "center", prop: "orderNum" },
        { label: "实际出库数量", align: "center", prop: "outStore", width: "100" },
        { label: "发票类型", align: "center", prop: "invoiceType" },
        { label: "发票号", align: "center", prop: "invoice" },
        { label: "操作人", align: "center", prop: "updateBy" },
        { label: "备注", align: "center", prop: "remark" },
      ],
      // colData中列出表格中的每一列，默认都展示
		  colData: [
        { title: "单据编码", isTrue: true },
        { title: "出库时间", isTrue: true },
        { title: "状态", isTrue: true },
        { title: "客户名称", isTrue: true },
        { title: "客户部门", isTrue: true },
        { title: "产品编码", isTrue: true },
        { title: "SKU编码", isTrue: true },
        { title: "产品名称", isTrue: true },
        { title: "规格", isTrue: true },
        { title: "批次", isTrue: true },
        { title: "单位", isTrue: true },
        { title: "订单数量", isTrue: true },
        { title: "实际出库数量", isTrue: true },
        { title: "发票类型", isTrue: true },
        { title: "发票号", isTrue: true },
        { title: "操作人", isTrue: true },
        { title: "备注", isTrue: true }
      ],
      // 当前选中的多选框，代表当前展示的列
      checkedColumns: ["单据编码", "出库时间", "状态", "客户名称", "客户部门", "产品编码", "SKU编码", "产品名称", "规格", "批次", "单位", "订单数量", "实际出库数量", "发票类型", "发票号", "操作人", "备注"],
      // 多选框的列表，列出表格的每一列
      checkBoxGroup: ["单据编码", "出库时间", "状态", "客户名称", "客户部门", "产品编码", "SKU编码", "产品名称", "规格", "批次", "单位", "订单数量", "实际出库数量", "发票类型", "发票号", "操作人", "备注"]
    }
  },
  created() {
    this.getList()
  },
  watch: {
    checkedColumns(val) {
      let arr = this.checkBoxGroup.filter(i => !val.includes(i));
      this.colData.filter(i => {
        if (arr.indexOf(i.title) != -1) {
          i.isTrue = false;
        } else {
          i.isTrue = true;
        }
      });
      this.reload = Math.random()
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listOutSale(this.addDateRange(this.queryParams)).then(res => {
        this.salesList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.time = ''
      this.queryParams.startTime = ''
      this.queryParams.endTime = ''
      this.handleQuery()
    },
    // 查看详情
    handleView(row) {
      this.$router.push({path:"/sales/outSale/detail",query:{id: row.id}})
    },
    // 时间选择器
    dataTime(){
      if(this.time){
        let day = 24 * 60 * 60 * 1000
        this.queryParams.startTime = this.parseTime(this.time[0])
        this.queryParams.endTime = this.parseTime(this.time[1].getTime() + day)
      }else if(this.time == null){
        this.queryParams.startTime = ''
        this.queryParams.endTime = ''
      }
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.map(item => item.id)
    },
    // 导出
    btnExport(){
      let a = this.colData.filter(i => {
        return i.isTrue == true
      })
      let b = []
      a.forEach((item) => {
        this.tableHeaderData.forEach((data) => {
          if(item.title == data.label){
            b.push({name: data.prop, value: item.title})
          }
        })
      })
      const obj = b.reduce((result, item) => {
        result[item.name] = item.value
        return result
      }, {})
      if(this.ids.length > 0){
        let data = {
          ids: this.ids,
          headMap: obj,
          batchNumber: this.queryParams.batchNumber,
          customerName: this.queryParams.customerName,
          businessCode: this.queryParams.businessCode,
          updateBy: this.queryParams.updateBy,
          status: this.queryParams.status,
          startTime: this.queryParams.startTime ? this.queryParams.startTime : undefined,
          endTime: this.queryParams.endTime ? this.queryParams.endTime : undefined
        }
        this.getExport(data)
      }else{
        let data = {
          headMap: obj,
          batchNumber: this.queryParams.batchNumber,
          customerName: this.queryParams.customerName,
          businessCode: this.queryParams.businessCode,
          updateBy: this.queryParams.updateBy,
          status: this.queryParams.status,
          startTime: this.queryParams.startTime ? this.queryParams.startTime : undefined,
          endTime: this.queryParams.endTime ? this.queryParams.endTime : undefined
        }
        this.getExport(data)
      }
    },
    getExport(data){
      this.$modal.loading("正在导出")
      exportProductOutOrder(data).then(res => {
        this.$modal.closeLoading()
        let blob = new Blob([res], {
          type: res.type,
        })
        let downloadElement = document.createElement("a")
        let href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = "销售出库.xls";
        document.body.appendChild(downloadElement)
        downloadElement.click()
        window.URL.revokeObjectURL(href)
      })
    }
  }
}
</script>

<style scoped>
.filter-table-col .el-icon-c-scale-to-original{
  font-size: 20px;
  cursor: pointer;
}
::v-deep .el-table__body .el-table__cell{
  padding:0;
}
::v-deep .el-table__body .cell{
  padding: 10px 0;
}
.item{
  height: 25px;
  margin-bottom: 10px;
}
.item:last-child{
  margin-bottom: 0;
}
</style>
